//
//  DMNewsVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/14.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMNewsVC.h"
#import "DMNewsFirstCell.h"
#import "DMNewsSecondCell.h"

@interface DMNewsVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger selectIndex;//1个人消息2系统消息
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UILabel *personLb;
@property (weak, nonatomic) IBOutlet UILabel *systemLb;
@property (nonatomic, strong) UIView *sliderView;//滑块

@property (nonatomic, strong) NSMutableArray *dataArray;




@end

@implementation DMNewsVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //创建动画
    CATransition *animation = [CATransition animation];
    //设置运动轨迹的速度
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    //设置动画类型
    animation.type = kCATransitionFade;
    //设置动画时长
    animation.duration =0.3f;
    //设置运动的方向
    animation.subtype =kCATransitionFromRight;
    //控制器间跳转动画
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    self.navigationItem.title = @"消息";
    selectIndex = 1;//默认个人消息
    _dataArray = [NSMutableArray new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMNewsFirstCell" bundle:nil] forCellReuseIdentifier:@"DMNewsFirstCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DMNewsSecondCell" bundle:nil] forCellReuseIdentifier:@"DMNewsSecondCell"];
    
    //滑块
    [self makeSliderView];
}

#pragma mark - tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (selectIndex == 1) {
        return 10;
//        return _dataArray.count;
    }
    if (selectIndex == 2) {
        return 3;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (selectIndex == 2) {
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 100;
        return self.tableView.rowHeight;
    }
    return 124;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (selectIndex == 2) {
        DMNewsSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMNewsSecondCell" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"DMNewsSecondCell" owner:self options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    DMNewsFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMNewsFirstCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMNewsFirstCell" owner:self options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

//左滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (selectIndex == 2) {
        return NO;
    }
    return YES;
}
// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
    }
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

#pragma mark - 头部选择相关
- (IBAction)selectTypeClick:(UIButton *)sender {
    selectIndex = sender.tag;
    if (selectIndex == 2) {
        _personLb.textColor = RGBCOLOR(85, 85, 85);
        _systemLb.textColor = DMMainColor;
    } else {
        _personLb.textColor = DMMainColor;
        _systemLb.textColor = RGBCOLOR(85, 85, 85);
    }
    CGFloat x = (selectIndex-1)*ScreenWidth/2;
    [UIView animateWithDuration:0.3 animations:^{
        _sliderView.frame = CGRectMake(x, 48, ScreenWidth/2, 2);
    }];
    [_tableView reloadData];
}
-(void)makeSliderView {
    _sliderView = [[UIView alloc]initWithFrame:CGRectMake(0, 48, ScreenWidth/2, 2)];
    _sliderView.backgroundColor = DMMainColor;
    [_headView addSubview:_sliderView];
}

@end

